package cn.edu.nju.software.gui;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.edu.nju.software.net.*;

	public class UserLand extends JFrame implements ActionListener{
		Connection connection;
		String userNameIn,passwordIn;
		JTextField userName;
		JPasswordField password;
		LogType type;
		UserInstitute uin;//��ݱ��ؿͻ��˵��������ж��û�������Ժϵ����ͬԺϵ�Ŀͻ��˳�ʼ��ʱֱ�Ӹ���ͬ��ֵ������
		public UserLand(){
			super();
			setLocationRelativeTo(null);
			final BorderLayout borderLayout = new BorderLayout();
			
			getContentPane().setLayout(borderLayout);//���Frame�Ĳ���
			setTitle("User Land");
			final JPanel panel_text = new JPanel();//������������textfield�����
			final GridLayout gridLayout = new GridLayout(2,2);//��ʱ������ǰ��Ӹ�ͷ���Կ��   
			//�����label
			gridLayout.setHgap(0);
			gridLayout.setVgap(5);
			panel_text.setLayout(gridLayout);
			ImageIcon userNameImage = new ImageIcon("image/userName.jpg");
			final JLabel userNameLabel = new JLabel("UserName",userNameImage,SwingConstants.RIGHT);
			userName = new JTextField(20);
			panel_text.setBackground(Color.WHITE);
			ImageIcon passwordImage = new ImageIcon("image/password.jpg");
			final JLabel passwdLabel = new JLabel("Password",passwordImage,SwingConstants.RIGHT);
			password = new JPasswordField (20);
			password.setEchoChar('*');
		    panel_text.add(userNameLabel);
		    panel_text.add(userName);
		    panel_text.add(passwdLabel);
		    panel_text.add(password);
		    
		    	
			JLabel label_image = new JLabel();
			label_image.setSize(400,80);
			ImageIcon loginImage = new ImageIcon("image/login.JPG");
			label_image.setIcon(loginImage);
			JButton login = new JButton("Log in");
			login.setBackground(Color.orange);
			login.setSize(40,20);
			JPanel button_panel = new JPanel();
			button_panel.add(login);
			button_panel.setBackground(Color.WHITE);
			add(label_image,borderLayout.NORTH);
			add(panel_text,borderLayout.CENTER);
			add(button_panel,borderLayout.SOUTH);
			
			try{
				connection = Connection.getInstance();//��������
			}
			catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			login.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					userNameIn = userName.getText();
					passwordIn = password.getText();//�Ѿ���ʱ   getPassword�õ�һ��char[]�����Ƽ���
					try{
						type = connection.handleLog(userNameIn,passwordIn);//���û�������뷢��ȥ
						}
					catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					switch(type){
					case Administrator:{
						setVisible(false);
						ManagerOperation mo = new ManagerOperation(userNameIn);
						System.out.println("生成了");
						mo.setUsername(userNameIn);
					}
					case Student:{
						setVisible(false);
						StudentsOperation so = new StudentsOperation(userNameIn);
						so.setUsername(userNameIn);
					}
					case LogFail:{
						JOptionPane.showMessageDialog(null, "Please check your input!");
			            userName.setText("");
			            password.setText("");   
						}
					// ������String��취����ȥ�͵ȴ��Ӧ
				
						
					}
					
			
				}
				
			});//Ϊ��ťע������¼�
			
			
		}
		public static void main(String args[]){
			UserLand land = new UserLand();
			land.setSize(400,240);
			land.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			land.setVisible(true);
			
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}

